package com.bookshop.controller;

import com.bookshop.entity.Collect;
import com.bookshop.entity.User;
import com.bookshop.entity.response.R;
import com.bookshop.service.CollectService;
import com.bookshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName: CollectController
 * Package: com.bookshop.controller
 * Description:收藏管理
 *
 * @Author BJX
 * @Create 2024/1/21 11:23
 * @Version 1.0
 */
@RestController
@RequestMapping("/collect")
public class CollectController {

    private CollectService collectService;
    private UserService userService;

    @Autowired
    public CollectController(CollectService collectService, UserService userService) {
        this.collectService = collectService;
        this.userService = userService;
    }

    @PostMapping("/addCollect")
    public R Collect(@RequestParam long book_id, HttpServletRequest request) {
        String phoneNumber = (String) request.getAttribute("phoneNumber");
        User user = userService.getUserByPhoneNumber(phoneNumber);

        int result = collectService.addCollect(book_id,user.getId());
        //int result = collectService.addCollect(book_id, 1);

        if (result == -1) {
            return R.error().message("收藏夹已存在该图书！");
        }

        return R.ok().message("收藏成功！");
    }

    @PostMapping("/cancelCollect")
    public R cancelCollect(@RequestParam long book_id, HttpServletRequest request) {
        String phoneNumber = (String) request.getAttribute("phoneNumber");
        User user = userService.getUserByPhoneNumber(phoneNumber);

        int result = collectService.cancelCollect(book_id,user.getId());
        //int result = collectService.cancelCollect(book_id, 1);

        if (result == -1) {
            return R.error().message("收藏夹没有该图书！");
        }

        return R.ok().message("取消收藏成功！");
    }

    @GetMapping("/countCollect")
    public R countCollect(HttpServletRequest request) {
        String phoneNumber = (String) request.getAttribute("phoneNumber");
        User user = userService.getUserByPhoneNumber(phoneNumber);

        int result = collectService.countCollect(user.getId());
        //int result = collectService.countCollect(1);

        return R.ok().put("count", result);
    }

    @PostMapping("/clearCollect")
    public R clearCollect(HttpServletRequest request) {
        String phoneNumber = (String) request.getAttribute("phoneNumber");
        User user = userService.getUserByPhoneNumber(phoneNumber);

        int result = collectService.clearCollect(user.getId());
        //int result = collectService.clearCollect(1);

        if (result > 0) {
            return R.ok().message("已清空收藏！");
        } else if (result == 0) {
            return R.error().message("收藏夹为空！");
        }
        return R.error().message("清空失败！");
    }

    @GetMapping("/showCollect")
    public R showCollect(@RequestParam(defaultValue = "1") int pageNum, HttpServletRequest request) {

        String phoneNumber = (String) request.getAttribute("phoneNumber");
        User user = userService.getUserByPhoneNumber(phoneNumber);

        List<Collect> collects = collectService.showCollect(pageNum, user.getId());
        //List<Collect> collects = collectService.showCollect(pageNum,1);

        if(collects.size()==0){
            return R.ok().message("收藏夹为空！");
        }
        return R.ok().put("data",collects);
    }

}
